package com.cj.web.back;

import com.cj.entity.CourseUser;
import com.cj.entity.PageBean;
import com.cj.service.CourseUserService;
import com.cj.utils.JSONUtils;
import com.cj.utils.ResultVO;
import com.cj.web.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: chenjie
 * @Date: 2021/7/12 - 07 - 12 - 17:15
 * @Description: com.cj.web.back
 */
@WebServlet("/courseUser")
public class CourseUserServlet extends BaseServlet {
    ResultVO vo ;
    CourseUserService service =   new CourseUserService();
    public void findPages(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        PageBean<CourseUser> pb = service.findPages(currentPage,pageSize);

        JSONUtils.objToJson(pb,resp);
    }

    public void updateCourseUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long uid = Long.valueOf(req.getParameter("uid"));
        Long cid = Long.valueOf(req.getParameter("cid"));
        Long id = Long.valueOf(req.getParameter("id"));


        CourseUser courseUser = new CourseUser(id, cid, uid);

        int row = service.updateCourseUser(courseUser);

        if(row >0){
            vo = new ResultVO(200,"修改成功",null);
        }else{

            vo = new ResultVO(500,"修改失败",null);
        }
        JSONUtils.objToJson(vo,resp);
    }

    //批量删除
    public void deleteCourseUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ids = req.getParameter("ids");

        int row = service.deleteCourseUser(ids);

        if(row >0){
            vo = new ResultVO(200,"批量删除成功",null);
        }else{

            vo = new ResultVO(500,"批量删除失败",null);
        }
        JSONUtils.objToJson(vo,resp);
    }
}
